package com.xfp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xfp.anntation.AutoFill;
import com.xfp.enumclass.OperationClass;
import com.xfp.mapper.NodeMapper;
import com.xfp.method.pageTotal;
import com.xfp.pojo.dto.NodeDto;
import com.xfp.pojo.dto.newNodeDto;
import com.xfp.pojo.entity.Business;
import com.xfp.pojo.entity.Node;
import com.xfp.pojo.entity.Region;
import com.xfp.pojo.entity.Vm;
import com.xfp.pojo.vo.*;
import com.xfp.service.NodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class NodeServiceImp implements NodeService {

    @Autowired
    private NodeMapper nodeMapper;

    /**
     * 点位搜索
     * @param dto
     * @return
     */
    @Override
    public RegionDetailedQueryVo pageNode(NodeDto dto) throws Exception {
        //进行安全校验
        dto.check();
        //使用pagehelper插件
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        //获取基础数据
        Page<Node> page = nodeMapper.pageNode(dto);
        //补充完整数据
        page.forEach(p->{
            String regionId = p.getRegionId();
            Integer businessId = p.getBusinessId();
            Region re = nodeMapper.findRegionById(regionId);
            Business bu = nodeMapper.findBusinessById(businessId);
            Integer n = nodeMapper.getVmCount(p.getId());
            p.setVmCount(n);
            p.setRegion(re);
            p.setBusinessType(bu);
        });
        //计算总页数
        Long total = pageTotal.total(page.getTotal(), dto.getPageSize());
        RegionDetailedQueryVo vo = RegionDetailedQueryVo.builder()
                .pageIndex(dto.getPageIndex())
                .pageSize(dto.getPageSize())
                .totalPage(total)
                .totalCount(page.getTotal())
                .currentPageRecords(page.getResult())
                .build();
        log.info("接收到的node列表为：{}",vo);
        return vo;
    }

    /**
     * 查询商圈列表
     * @return
     */
    @Override
    public BusinessVo findBusinessList() {
        List<Business> list = nodeMapper.findBusinessList();
        BusinessVo vo = new BusinessVo();
        vo.setCurrentPageRecords(list);
        return vo;
    }

    /**
     * 新增点位
     * @param dto
     */
    @Override
    public void insertNode(newNodeDto dto) throws Exception {
        int n = nodeMapper.insertNode(dto);
        if(n!=1){
            throw new Exception("新增点位失败");
        }
    }

    /**
     * 修改点位
     * @param id
     * @param dto
     */
    @Override
    public void updateNode(String id, newNodeDto dto) {
        dto.setNodeId(id);
        int n = nodeMapper.updateNode(dto);
    }

    /**
     * 删除点位
     * @param nodeId
     */
    @Override
    public void delete(String nodeId) {
        nodeMapper.delete(nodeId);
    }

    /**
     * 点位详情
     * @param id
     * @return
     */
    @Override
    public NodeVmListVo findNode(String id) {
        List<Vm> list  = nodeMapper.findVmById(id);
        NodeVmListVo vo = new NodeVmListVo();
        vo.setVmList(list);
        return vo;
    }

}
